package com.express.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.express.entity.Address;
import com.express.mybatis.PageResult;
import com.express.mybatis.Result;
import com.express.service.IAddressService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 取件人信息控制类
 */
@RestController
@RequestMapping({"/pick"})
public class PickupController {

	@Autowired
	IAddressService addressService;

	@PostMapping({"/list"})
	public PageResult list(@RequestBody PageResult page) {
		PageResult result = new PageResult();
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		Map<String,Object> map = new HashMap<>();
		map.put("type",1);
		List<Address> list = addressService.query(map);
		result.setList(list);
		return result;
	}


	@PostMapping({"/add"})
	public Result add(@RequestBody Address address) {
		Result result = new Result();
		this.addressService.save(address);
		return result;
	}

	@PostMapping({"/update"})
	public Result update(@RequestBody Address address) {
		Result result = new Result();
		address.setUpdateDate(new Date());
		this.addressService.update(address);
		return result;
	}

	@PostMapping({"/delete"})
	public Result delete(@RequestBody JSONObject json) {
		Result result = new Result();
		String[] idsArr = json.getString("ids").split(",");
		int[] idsInt = new int[idsArr.length];
		for (int i = 0; i < idsArr.length; i++) {
			idsInt[i] = Integer.parseInt(idsArr[i]);
		}
		this.addressService.deleteByIds(idsInt);
		return result;
	}
}
